import SceneManager from "../../manager/SceneManager";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class MainScene extends cc.Component {

    @property(cc.Node)
    cloudLayer:cc.Node = null;

    @property(cc.Node)
    mainControll:cc.Node = null;
    private onEnterHeroInfo() {
        SceneManager.instance.goto("HeroInfoScene")
    }
    private onEnterFight()
    {
        SceneManager.instance.goto("FightScene")
    }
    //云层移动效果
    private onCloudLayerEffect() {
        let action:cc.ActionInterval = cc.repeatForever(cc.rotateBy(240,360))
        this.cloudLayer.runAction(action)
    }
    private isPlayingAction:boolean = false;
    private rotationArr:Array<number> = [0,74,145,220,290]
    private selectIndex:number = 0;
    private onMoveMainControll() {
        this.isPlayingAction = true;
        let callFunc:cc.ActionInstant = cc.callFunc(function(){
            this.isPlayingAction = false;
        },this)
        let rotation:number = this.rotationArr[this.selectIndex]
        let action:cc.ActionInterval = cc.sequence(cc.rotateTo(1,rotation),callFunc)
        this.mainControll.runAction(action)
    }
    private xStart:number = 0;
    private isMoved:boolean = false;
    private MOVE_OFFSET = 50;
    private onTouchScreen(event:cc.Event.EventTouch) {
        if (event.type==cc.Node.EventType.TOUCH_START)
        {
            this.xStart = event.getLocationX();
        }
        if (event.type==cc.Node.EventType.TOUCH_MOVE)
        {
            let xMove:number = event.getLocationX()
            let xOffset:number = xMove-this.xStart
            if (Math.abs(xOffset)>=this.MOVE_OFFSET&&!this.isMoved&&!this.isPlayingAction)
            {
                this.isMoved = true
                if (xOffset>=this.MOVE_OFFSET)
                {
                    this.selectIndex = this.selectIndex+1
                }
                if (xOffset<=-this.MOVE_OFFSET)
                {
                    this.selectIndex = this.selectIndex-1
                }
                this.selectIndex = (this.selectIndex<0)&&(6+this.selectIndex)||(this.selectIndex%5)
                this.onMoveMainControll()
            }
        }
        if (event.type==cc.Node.EventType.TOUCH_END)
        {
            this.isMoved = false
        }
    }
    start () {
        this.onCloudLayerEffect()
        this.node.on(cc.Node.EventType.TOUCH_START,this.onTouchScreen,this)
        this.node.on(cc.Node.EventType.TOUCH_MOVE,this.onTouchScreen,this)
        this.node.on(cc.Node.EventType.TOUCH_END,this.onTouchScreen,this)
    }

}
